package view;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import controller.DeleteUser;
import controller.TableModelUser;

@SuppressWarnings("serial")
public class PanelUserList extends JPanel {

	private static PanelUserList single;
	private String user_name;

	JTable UserList = new JTable(new TableModelUser());
	JScrollPane JSP = new JScrollPane(UserList);

	private JButton registerUB = new JButton("Nieuwe gebruiker");

	private JButton deleteUB = new JButton("Verwijder");
	private JButton deleteUB2 = new JButton("Bevestigen");

	private PanelUserList() {

		setBounds(0, 0, 860, 720);
		setLayout(null);
		UserList.setRowSelectionInterval(0, 0);
		JSP.setBounds(280, 100, 280, 500);
		registerUB.setBounds(1, 100, 140, 40);
		deleteUB.setBounds(1, 150, 140, 40);
		deleteUB2.setBounds(1, 150, 140, 40);
		
		deleteUB2.setBackground(Color.RED);
		deleteUB2.setVisible(false);
		
		UserList.addFocusListener(new FocusListener() {
			
			@Override
			public void focusLost(FocusEvent e) {
				deleteUB.setVisible(true);
				deleteUB2.setVisible(false);
				
			}
			
			@Override
			public void focusGained(FocusEvent e) {
				deleteUB.setVisible(true);
				deleteUB2.setVisible(false);
				
			}
		});
		
		registerUB.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				PanelHolder mf = PanelHolder.getInstance();
				mf.remove(PanelUserList.getInstance());
				mf.add(Panelregister.getInstance());
				mf.repaint();
				mf.validate();
				
			}
		});

		deleteUB.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				deleteUB.setVisible(false);
				deleteUB2.setVisible(true);
				
			}
		});
	
		
		deleteUB2.addActionListener(new DeleteUser(this));
		deleteUB2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				deleteUB.setVisible(true);
				deleteUB2.setVisible(false);
				
			}
		});

		add(JSP);
		add(registerUB);
		add(deleteUB);
		add(deleteUB2);

	}

	public static PanelUserList getInstance() {
		if (single == null) {
			single = new PanelUserList();
		}
		return single;
	}

	public void selfDestroy() {
		single = null;

	}

	public JTable getUserList() {
		return UserList;
	}

	public void setUserList(JTable userList) {
		UserList = userList;
	}

}